package rateexchange.client.mvp;

import java.util.List;

import rateexchange.client.ClientFactory;
import rateexchange.shared.RateProxy;
import rateexchange.shared.RateRequest;

import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;

//Provides data for the CellTable listing Rates
public class RateDataProvider extends AsyncDataProvider<RateProxy>{
	private ClientFactory clientfactory;
	
	public RateDataProvider(ClientFactory clientfactory) {
		this.clientfactory = clientfactory;
	}
	
	@SuppressWarnings("deprecation")
	@Override
//	Called when a display(CellTable containing rates) changes its range of interest also when CellTable is provided with the DataProvider first time.
	protected void onRangeChanged(HasData<RateProxy> display) {
		final Range range = display.getVisibleRange();
		RateRequest ratesreq = clientfactory.getRequestFactory().raterequest();
		//firing the request with handed over Receiver for async call.
		ratesreq.findRates(range.getStart(), range.getLength()).fire(new Receiver<List<RateProxy>>(){

			@Override
			public void onSuccess(List<RateProxy> response) {
				if (response.size()>0){
					updateRowData(range.getStart(), response);
				}
				
			}
			
		});
		
	}

}
